package rocketmq.transaction.controller;

import org.apache.rocketmq.client.producer.LocalTransactionState;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TransMsgController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @GetMapping(value = "/send/half")
    public String sendHalf(String name) {
        Message<String> msg = MessageBuilder.withPayload(name).build();
        TransactionSendResult result = rocketMQTemplate.sendMessageInTransaction("tx_producer_group", msg, "name");
        LocalTransactionState state = result.getLocalTransactionState();
        if (state == LocalTransactionState.COMMIT_MESSAGE) {
            return "send half success";
        }else if (state == LocalTransactionState.ROLLBACK_MESSAGE) {
            return "send half fail";
        }else{
            return "send half unknown";
        }
    }
}
